/**
 * @author erond
 * @class Classe adibita all'avvio di un Ftp Server sulla specifico indirizzo, porta e con
 *        lo specifico file di configurazione
 */

package commonUtility;

import java.io.File;
import java.util.Vector;

import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;

public class FtpServerThread extends Thread{

	private String host = null;
	private int port = 0;
	private String config_file = null;

	public FtpServerThread(String host,int port,String config_file) {
		this.host=host;
		this.port=port;
		this.config_file=config_file;
	}

	public void run() {

		System.err.println(Thread.currentThread().getName()+" avviato su: \n indirizzo: "+host+"\n porta: "+port+"\n config_file: "+config_file);

		FtpServerFactory serverFactory = new FtpServerFactory();
		ListenerFactory factory = new ListenerFactory();

		// configure the listener
		try {
			factory.setPort(port);
			factory.setServerAddress(host);
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		// replace the default listener
		serverFactory.addListener("default", factory.createListener());

		// create the server
		FtpServer server = serverFactory.createServer(); 

		// set the local configuration file and user manager associated with the file
		PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
		userManagerFactory.setFile(new File(config_file));
		serverFactory.setUserManager(userManagerFactory.createUserManager());

		//start the server
		try {
			server.start();
		} catch (FtpException e) {
			e.printStackTrace();
		}
	}//fine run

	//main
	public static void main(String args[]) {

		System.out.println("Ftp avviato. Configurazione in corso...");

		Object semaphore = new Object();

		FtpServerConfig ftpConfig = new FtpServerConfig(semaphore);
		ftpConfig.setLocationRelativeTo(null);
		ftpConfig.setVisible(true);

		synchronized(semaphore) {
			try {
				semaphore.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		Vector <String[]> contents = ftpConfig.getContents();
		ftpConfig.dispose();

		System.out.println("Ftp: configurazione terminata!");

		String[] tmp;

		tmp = contents.elementAt(0);
		if (tmp[2].equalsIgnoreCase("true")) {
			FtpServerThread master_esterno = new FtpServerThread(tmp[0], Integer.parseInt(tmp[1]), "./res/conf/master_users.properties");
			master_esterno.start();
		}

		tmp = contents.elementAt(1);
		if (tmp[2].equalsIgnoreCase("true")) {
			FtpServerThread master = new FtpServerThread(tmp[0], Integer.parseInt(tmp[1]), "./res/conf/master_users.properties");
			master.start();
		}

		tmp = contents.elementAt(2);
		if (tmp[2].equalsIgnoreCase("true")) {
			FtpServerThread slave_esterno = new FtpServerThread(tmp[0], Integer.parseInt(tmp[1]), "./res/conf/slave_users.properties");
			slave_esterno.start();
		}

		tmp = contents.elementAt(3);
		if (tmp[2].equalsIgnoreCase("true")) {
			FtpServerThread slave = new FtpServerThread(tmp[0], Integer.parseInt(tmp[1]), "./res/conf/slave_users.properties");
			slave.start();
		}
	}

}
